<?php

namespace App\Module\Sub;

use App\depository\ManageMapExtraDep;
use App\depository\SubAuthorityDep;
use App\Enum\MapEnum;
use App\Module\BaseModule;
use App\depository\ManageMapDep;
use App\Service\DictService;

class SubAuthorityModule extends BaseModule
{
    public function init($request)
    {
        $dictService = new DictService();

        $teamId = $request->headers->get('TeamId');
        $dictService->setAuthorityGroup( $teamId);

        $data['dict'] = $dictService->getDict();

        return self::response($data);
    }

    public function add($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        if(empty($param['steamid'])){
            return self::response([],'请输入SteamId',100);
        }

        if(empty($param['group'])){
            return self::response([],'请指定权限组',100);
        }

        $data = [
            'team_id' => $teamId,
            'steamid' => $param['steamid'],
            'remark' => $param['remark'] ?? "",
            'group' => $param['group']
        ];

        $dep = new SubAuthorityDep();
        $dep->add($data);

        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        if(empty($param['steamid'])){
            return self::response([],'请输入SteamId',100);
        }

        if(empty($param['group'])){
            return self::response([],'请指定权限组',100);
        }

        $data = [
            'steamid' => $param['steamid'],
            'remark' => $param['remark'] ?? "",
            'group' => $param['group']
        ];

        $dep = new SubAuthorityDep();
        $dep->upd($param['id'],$data);

        return self::response();

    }

    public function del($request){

        $param = $request->input();

        $dep = new SubAuthorityDep();
        $dep->del($param['id']);

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        $dep = new SubAuthorityDep();

        $resList = $dep->list($teamId, $param);
        $list = $resList->getCollection()->toBase();
        $data['list'] = $list->map(function ($item) {
            return [
                'id' => $item->id,
                'steamid' => $item->steamid,
                'group' => str_replace('Group=','',$item->group),
                'remark' => $item->remark,
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }

}

